package com.niuma.filter;

import com.niuma.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 一颗蛋50斤
 */
public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        if (request.getSession().getAttribute(Constants.USER_SESSION) == null) {
            // 获取当前项目的路径
            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath+"/login.jsp");
        }
        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
